<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:90px' }"
    :wrapper-col="{ style: 'calc(100% - 90px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="仓库名称" name="cnName" :rules="[{ required: true, message: '请填写仓库名称!' }]">
      <a-input v-model:value="formState.cnName" :maxlength="20" placeholder="请输入" />
    </a-form-item>
    <a-form-item label="仓库负责人" name="warehouseManagerId" :rules="[{ required: true, message: '请选择仓库负责人!' }]">
      <a-select v-model:value="formState.warehouseManagerId" placeholder="请选择" @change="changeWarehouseManagerHandle">
        <a-select-option v-for="item in userOpts" :key="item.id" :value="item.id">{{ item.username }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="省/市/区" name="cityModel" :rules="[{ required: true, message: '请选择省/市/区!' }]">
      <a-cascader
        v-model:value="formState.cityModel"
        :fieldNames="{ label: 'name', value: 'code', children: 'child' }"
        @change="changeCityModelHandle"
        :options="baseStore.provincesOpts"
        :show-search="{ filter }"
        placeholder="请选择"
      />
    </a-form-item>
    <a-form-item label="地址" name="address" :rules="[{ required: true, message: '请填写地址!' }]">
      <a-textarea v-model:value="formState.address" show-count :maxlength="50" placeholder="请输入" />
    </a-form-item>
    <a-form-item label="状态" name="status" :rules="[{ required: true, message: '请选择状态!' }]">
      <a-select v-model:value="formState.status" placeholder="请选择">
        <a-select-option :value="1">启用</a-select-option>
        <a-select-option :value="0">停用</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button type="primary" html-type="submit">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { toRefs, watch } from "vue";
import baseStore from "@/store/modules/base";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: WarehouseManagement.ListRows;
    type: number;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
}>();
const { visible, passData, type } = toRefs(props);
const { formDataRef, formState, filter, userOpts, getUserOpts } = InitHook();
const { onFinish, onFinishFailed, resetFields, resetHandle, changeWarehouseManagerHandle, changeCityModelHandle } = OperationHook({ formDataRef, formState, passData, emit, type, userOpts });
const init = () => {
  if (type.value === 2) {
    Object.assign(formState, passData.value);
    formState.cityModel = [passData.value.state, passData.value.city, passData.value.zone];
  }
  getUserOpts();
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (v) {
      init();
    } else {
      resetFields();
      formDataRef.value.clearValidate();
    }
  }
);
init();
</script>
<style lang="stylus" scoped></style>
